<?php

namespace App\Http\Controllers\Internal\Shop;

use App\Http\Controllers\BaseController;
use App\Models\Cruise\CruiseCheckAgainstModel;
use App\Models\Cruise\CruiseShopAccountModel;
use App\Service\Cruise\Check\CruiseOrderService;
use App\Service\Cruise\CruiseTour\CruiseTourService;
use App\Service\Cruise\Shop\CruiseShopLicenseRecordsService;
use App\Service\OpenShop\Feishu\MainProcessService;
use App\Service\OpenShop\Feishu\OrganizationManageService;
use App\Service\OpenShop\FeishuMini\ShopService;
use App\Service\Renew\RenewOrderService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class ShopController extends BaseController
{
    public function orderRatio(Request $request)
    {
        $pageSize = $request->get('pageSize', 100);
        $pageNum  = $request->get('pageNum', 1);

        $this->validate($request, [
            'pageSize' => 'integer|min:0|max:1000',
            'pageNum'  => 'integer|min:1',
        ]);

        $data = CruiseShopAccountModel::query()
                                      ->forPage($pageNum, $pageSize)
                                      ->select('shopId', 'turnover', 'orderAmount')
                                      ->get()
                                      ->map(function ($item) {
                                          if ($item->turnover <= 0) {
                                              $item['orderRatio'] = 0;
                                          } else {
                                              $item['orderRatio'] = round($item->orderAmount / $item->turnover * 10000) / 100;
                                          }

                                          $item['lastCruiseTime'] = time();

                                          return $item;
                                      })
                                      ->toArray();

        return $this->success($data);
    }

    public function lastCruiseTime(Request $request)
    {
        $pageSize = $request->get('pageSize', 100);
        $pageNum  = $request->get('pageNum', 1);

        $this->validate($request, [
            'pageSize' => 'integer|min:0|max:1000',
            'pageNum'  => 'integer|min:1',
        ]);

        $data = CruiseOrderService::lastCruiseTime($pageSize, $pageNum);

        return $this->success($data);
    }

    public function nowStepStatus(Request $request)
    {
        $shopIds = $request->get('shopIds');

        $this->validate($request, [
            'shopIds' => 'required|array',
        ]);

        return $this->success(MainProcessService::getMainByShopIds($shopIds));
    }

    /**
     * 获取省总
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function getOrganizationInfoByType(Request $request): JsonResponse
    {
        $type         = $request->get('type', 1);
        $provinceCode = $request->get('provinceCode', 0);

        $this->validate(
            $request, [
                'type'         => 'integer|between:1,2',
                'provinceCode' => 'integer|min:0',
            ],
        );
        $data = OrganizationManageService::getOrganizationInfoByType($type, $provinceCode);
        return $this->success($data);
    }

    // rms shopLists辅助接口
    public function shopListsSub(Request $request)
    {
        $shopIds = $request->post('shopIds');

        $this->validate($request, [
            'shopIds' => 'required|array',
        ]);

        $data = [
            'nowStepStatus' => MainProcessService::getMainByShopIds($shopIds),
            'againstIds'    => CruiseCheckAgainstModel::getWaitAgainstIds($shopIds),
            'renewOrders' => RenewOrderService::getRenewOrdersByShopIds($shopIds)
        ];

        return $this->success($data);
    }

    /**
     * 根据门店ID获得培训校区
     * @param Request $request
     * @return JsonResponse
     */
    public function getTrainSchool(Request $request): JsonResponse
    {
        $shopId = $request->get('shopId', 0);
        return $this->success(ShopService::getTrainSchool($shopId));
    }

    /**
     * 判断是否是叁代店
     * @param Request $request
     * @return JsonResponse
     */
    public function getShopPatternByShopId(Request $request): JsonResponse
    {
        $shopId = $request->get('shopId', 0);
        return $this->success(ShopService::getShopPatternByShopId($shopId));
    }


    /**
     * 根据门店ID查询门店属性
     * @param Request $request
     * @return JsonResponse
     */
    public function getShopAttributeByShopIds(Request $request): JsonResponse
    {
        $shopIds = (string)$request->get('shopIds', '');
        $type    = (string)$request->get('type', '');
        $shopIds = explode(',', $shopIds);
        return $this->success(ShopService::getShopAttributeByShopIds($shopIds, $type));
    }

    /**
     * 通过门店code获得开店手机号
     * @param Request $request
     * @return JsonResponse
     */
    public function getShopMobileByShopCode(Request $request): JsonResponse
    {
        $shopId = (int)$request->get('shopCode', 0);
        return $this->success(ShopService::getShopMobileByShopCode($shopId));
    }

    /**
     * 根据稽核人员id查询对应门店区域
     * @param Request $request
     * @return JsonResponse
     */
    public function getShopCruiseManageArea(Request $request): JsonResponse
    {
        $userIds    = (string)$request->get('userIds', '');
        $regionCode = (string)$request->get('regionCode', '');
        $userIds    = $userIds ? explode(',', $userIds) : [];
        return $this->success(ShopService::getShopCruiseManageArea($userIds, $regionCode));
    }

    /**
     * 根据云巡店-稽核日期查询对应门店id
     * @param Request $request
     * @return JsonResponse
     */
    public function getShopIdsByTourCruise(Request $request): JsonResponse
    {
        $startTime = (int)$request->get('startTime', 0);
        $endTime   = (int)$request->get('endTime', 0);
        if (!$startTime || !$endTime) {
            return $this->success();
        }
        return $this->success(CruiseTourService::getShopIdsByTourCruise($startTime, $endTime));
    }

    public function getShopLicenseStatus(Request $request):JsonResponse
    {
        $shopIds = $request->get('shopIds', '');
        $shopIdsArr = explode(',',$shopIds);

        if (empty($shopIdsArr)){
            return $this->success();
        }

        return $this->success(CruiseShopLicenseRecordsService::getShopLicenseStatus($shopIdsArr));
    }
}
